import subprocess
import time

import pyautogui
from pynput import keyboard

from pynput.keyboard import Key, Controller as KeyboardController

# # 初始化键盘控制器
keyboard_controller = KeyboardController()

def switchToEn():


    # 获取当前输入法
    result = subprocess.run(
        ["im-select", "-c"],
        capture_output=True,
        text=True
    )

    if result == "com.apple.keylayout.ABC":
        subprocess.run(
            ["im-select", "com.apple.keylayout.ABC"],
            check=True
        )
    else:
        
        # 这里有坑, 按理说.run和ctrl+space只需要一个就可以触发切换了, 但是就是需要先.run, 再ctrl+space
        subprocess.run(
            ["im-select", "com.baidu.inputmethod.BaiduIM.pinyin"],
            check=True
        )
        pyautogui.hotkey('ctrl', 'space')

        subprocess.run(
            ["im-select", "com.apple.keylayout.ABC"],
            check=True
        )

        # 切换到英文输入



def switchToCn():

    # 获取当前输入法
    result = subprocess.run(
        ["im-select", "-c"],
        capture_output=True,
        text=True
    )

    if result == "com.baidu.inputmethod.BaiduIM.pinyin":
        subprocess.run(
            ["im-select", "com.baidu.inputmethod.BaiduIM.pinyin"],
            check=True
        )
    
    else:
        
        subprocess.run(
            ["im-select", "com.apple.keylayout.ABC"],
            check=True
        )
        pyautogui.hotkey('ctrl', 'space')

        subprocess.run(
            ["im-select", "com.baidu.inputmethod.BaiduIM.pinyin"],
            check=True
        )
    




